<?php

namespace app\controller;

use support\Request;
use support\Response;

class ApiController
{
    /**
     * 成功输出
     * @param array $data
     * @param string $msg
     * @param int $status
     * @param int $options
     * @return Response
     */
    public static function success($data = [], $msg = 'OK', $status = 1, $options = JSON_UNESCAPED_UNICODE)
    {
        $data = [
            'code' => $status,
            'msg' => $msg,
            'data' => $data
        ];
        return new Response(200, ['Content-Type' => 'application/json'], json_encode($data, $options));
    }

    /**
     * 失败输出
     * @param string $msg
     * @param int $status
     * @param array $data
     * @param int $options
     * @return Response
     */
    public static function error($msg = 'error', $status = 0, $data = [], $options = JSON_UNESCAPED_UNICODE)
    {
        $data = [
            'code' => $status,
            'msg' => $msg,
            'data' => $data
        ];
        return new Response(200, ['Content-Type' => 'application/json'], json_encode($data, $options));
    }

    /**
     * 获取每页分页数
     * @param $limit
     * @return int
     */
    public static function paramNum($limit=10)
    {
        $num = request()->param('num', $limit, 'intval');
        return $num > 100 ? 100 : $num;
    }
}
